<template>
	<view class="test-page">
		<view>{{ JSON.stringify(test) }}</view>
		<web-view v-if="mapshow" id="web-view-1" :src="src" @message="handleMessage"></web-view>
	</view>
</template>

<script>
	export default {
		onLoad(e) {
			console.log('eeeeeeeeee',e)
			this.webViewContext = my.createWebViewContext('web-view-1');
			console.log(e.lng,e.lat)
			let outArr = []
			if(e.fourArea) {
				e.fourArea = e.fourArea.split(',')
				
				let lngArr = []
				let latArr = []
				e.fourArea.forEach((item, index) => {
					item = Number(item)
					if((index+1)%2 == 0) {
						lngArr.push(item)
					}else{
						latArr.push(item)
					}
				})
				lngArr.forEach((item1, index1) => {
					outArr.push([latArr[index1],lngArr[index1]])
				})
				console.log(outArr, 'outArray')
			}
			e.type == 'check' ? this.src = this.src + '?lng='+ e.lng +'&lat=' +e.lat + '&type=check' : this.src = this.src + '?lng='+ e.lng +'&lat=' +e.lat
			console.log('this.src',this.src)
			console.log('this.src',outArr)
			// 向webview发送信息
			setTimeout(() => {
				
			}, 500),
			this.timer = setInterval(() => {
				// 向webview发送信息
				const path = outArr
				console.log(path,'pathhhhhhhhhhhhh')
				// const path = [[119.985074, 30.518515], [119.985782, 30.518658], [119.987128, 30.517909], [119.985696, 30.516426], [119.98516, 30.516865]]
				this.webViewContext.postMessage({ path });
			}, 200)
		},
		destroyed() {
			clearInterval(this.timer)
			this.timer = null
		},
		data() {
			return {
				// src: 'http://172.16.27.145:5500/index.html',
				src: 'https://zhatu.deqing.gov.cn/polygonDraw/index.html',
				webViewContext: null,
				mapshow: true,
				test: {},
				timer: null
			}
		},
		methods: {
			// webview向app发送信息
			handleMessage(evt) {
				console.log('收到webview信息', evt.detail)
				this.mapshow = false
				this.test = evt.detail.path
				this.$store.state.path = evt.detail.path
				this.$store.state.center = evt.detail.center
				this.$store.state.polygonArea = evt.detail.polygonArea
				uni.showToast({
					title: '四至范围选择完成',
					duration: 99
				})
				uni.navigateBack()
			},
		},
	}
</script>

<style lang="scss" scoped>
	.test-page {
		background-color: yellow;

		#web-view-1 {
			position: absolute;
			top: 100px;
			left: 0;
		}
	}
</style>
